package com.monkey.luffer.springboot.demo.basis.thread;

import java.util.ArrayList;
import java.util.List;

/**
 * @Author dengkaibing
 * @Description
 * @Date 2024/8/8 23:23
 **/
public class ProviderAndConsumeThread {

    private static List<String> list = new ArrayList<>();

    public static void main(String[] args) throws InterruptedException {
        Object lock = new Object();

        new Thread(() -> {
            synchronized (lock) {
                while (true) {
                    if (list.isEmpty()) {
                        lock.notifyAll();
                        try {
                            lock.wait();
                        } catch (InterruptedException e) {
                            e.printStackTrace();
                        }
                    }
                    System.out.println(list.get(0));
                    list.remove(0);
                }
            }

        }).start();

        new Thread(() -> {
            synchronized (lock) {
                while (true) {
                    if (list.size()==10) {
                        lock.notifyAll();
                        try {
                            lock.wait();
                        } catch (InterruptedException e) {
                            e.printStackTrace();
                        }
                    }
                    list.add("str");
                }
            }

        }).start();
        Thread.sleep(5000l);
    }
}
